package com.ruoyi.swla2.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.swla2.domain.WarnHistory;
import com.ruoyi.swla2.mapper.WarnHistoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.swla2.mapper.WarnMapper;
import com.ruoyi.swla2.domain.Warn;
import com.ruoyi.swla2.service.IWarnService;

/**
 * 告警Service业务层处理
 * 
 * @author gdqt
 * @date 2023-09-11
 */
@Service
@Slf4j
public class WarnServiceImpl implements IWarnService 
{
    @Autowired
    private WarnMapper warnMapper;

    @Autowired
    private WarnHistoryMapper warnHistoryMapper;

    // 误报参数
    public static final int FALSE_ALARM_STATUS = 2;

    /**
     * 查询告警
     * 
     * @param ID 告警主键
     * @return 告警
     */
    @Override
    public Warn selectWarnByID(Long ID)
    {
        return warnMapper.selectWarnByID(ID);
    }

    /**
     * 查询告警列表
     * 
     * @param warn 告警
     * @return 告警
     */
    @Override
    public List<Warn> selectWarnList(Warn warn)
    {
        return warnMapper.selectWarnList(warn);
    }

    /**
     * 新增告警
     * 
     * @param warn 告警
     * @return 结果
     */
    @Override
    public int insertWarn(Warn warn)
    {
        return warnMapper.insertWarn(warn);
    }

    /**
     * 修改告警
     *
     * @param warn 告警
     * @return 结果
     */
    @Override
    public int updateWarn(Warn warn)
    {
        log.info("执行了一次更新");
        int result = (warn.getWarnStatus() == FALSE_ALARM_STATUS)
                ? (updateWarnStatus(warn) ? 1 : 0)
                : warnMapper.updateById(warn);

        if (result <= 0) {
            log.error("更新告警失败, ID: {}", warn.getID());
        }
        return result;
    }

    /**
     * 批量删除告警
     * 
     * @param IDs 需要删除的告警主键
     * @return 结果
     */
    @Override
    public int deleteWarnByIDs(Long[] IDs)
    {
        return warnMapper.deleteWarnByIDs(IDs);
    }

    /**
     * 删除告警信息
     * 
     * @param ID 告警主键
     * @return 结果
     */
    @Override
    public int deleteWarnByID(Long ID)
    {
        return warnMapper.deleteWarnByID(ID);
    }

    @Override
    public Boolean updateWarnStatus(Warn warn) {
        try {
            // 实时报警映射到历史报警
            WarnHistory warnHistory = entityMapping(warn);
            // 历史报警添加
            warnHistoryMapper.insert(warnHistory);
            // 实时报警删除
            warnMapper.deleteById(warn);
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    @InterceptorIgnore(tenantLine = "true")
    public List<Warn> selectListForTask(List<Long> configIds, Date sTime, Date eTime) {
        return warnMapper.selectList(new LambdaQueryWrapper<Warn>().in(Warn::getTargetID,configIds)
                .eq(Warn::getHandleStatus,0)
                .ge(Warn::getStartTime,sTime)
                .le(Warn::getStartTime,eTime));
    }

    @Override
    @InterceptorIgnore(tenantLine = "true")
    public void updateForTask(Warn warn) {
        warnMapper.updateById(warn);
    }

    @Override
    @InterceptorIgnore(tenantLine = "true")
    public void insertForTask(Warn warn) {
        warnMapper.insert(warn);
    }

    /**
     * 把实时数据映射到历史数据，主键ID也要映射，确保流程中绑定的业务主键是同一个
     * @param warn
     * @return
     */
    public WarnHistory entityMapping(Warn warn){
        WarnHistory history = new WarnHistory();
        BeanUtils.copyProperties(warn, history);
        return history;
    }
}
